#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Configuration;
using System.ServiceModel;
using XcoAppSpaces.Contracts.Logging;
using Microsoft.ServiceBus;

namespace XcoAppSpaces.Transport.WCF
{
    /// <summary>
    /// Service that manages incoming and outgoing messages from/to remote spaces by use of WCF. Incoming
    /// messages are received by a hosted SpacePortal service. Outgoing messages are sent by RemoteSender 
    /// objects, the currently open RemoteSenders are managed in a list. A Timer regularly checks if any 
    /// connections to remote spaces have run into a timeout, and closes and removes such connections.
    /// </summary>
    public class XcoAzureTransportService : XcoWCFTransportService
    {

        private IXcoLogger log = null;

        /// <summary>
        /// Creates a new XcoAzureTransportService.
        /// </summary>
       public XcoAzureTransportService()
            : base()
        {
            AzureInit();
        }
       /// <summary>
       /// Creates a new XcoWCFTransportService.
       /// </summary>
       /// <param name="port">The port that should be used - the last part of the azure url.</param>
        public XcoAzureTransportService(int port)
            : base(BindingsHelperAzure.GenerateUri(port))
        {
            AzureInit();
           
        }
        /// <summary>
        /// Creates a new XcoWCFTransportService.
        /// </summary>
        /// <param name="address">The address that should be used.</param>
        public XcoAzureTransportService(string address)
            : base(address)
        {
            AzureInit();
        }
        private RemoteSender NewAzureRemoteSender(string remoteAddress)
        {
            return new RemoteSenderAzure(remoteAddress, log,this);
        }

        /// <summary>
        /// Initialize the azure service 
        /// </summary>
        private void AzureInit()
        {
           SetBindingHelper(new BindingsHelperAzure());
            log = XcoLoggerFactory.CreateInstance(this.GetType());
            ConfigLoader = new LoadHostConfigurationDelegate(LoadConfigurationAzure);
            CreateNewRemoteSender = new NewRemoteSenderDelegate(NewAzureRemoteSender);
        }

        /// <summary>
        /// Load specific configuration settings.       
        /// </summary>
        /// <param name="coreUri">Baseaddress for the azure-service</param>
        /// <param name="spacePortal">The space-portal instance for azure hosting</param>
        /// <returns>ServiceHost from the Azure-SpacePortal</returns>
        private ServiceHost LoadConfigurationAzure(Uri coreUri, IRemoteSpaceService spacePortal)
        {
            ServiceHost host = new ServiceHost(spacePortal, coreUri);
            return host;
        }

    }
}